{% extends 'df_goods/base.html' %}
{% block head %}
    <script type="text/javascript">
        function add(){
            num = parseInt($('.num_show').val());
            if(num<{{ goods.gstock }}){
                $('.num_show').val(num+1);
            }
            $('.num_show').blur();
        }
        function minus(){
            num = parseInt($('.num_show').val());
            if(num>1){
                $('.num_show').val(num-1);
            }
            $('.num_show').blur();
        }
        $(function(){
            $('.num_show').blur(function(){
                num = parseInt($('.num_show').val());
                if (num<1){
                    num=1;
                }
                $('.num_show').val(num);
                total = num*{{ goods.gprice }};
                $('.total em').html(total.toFixed(2) + '元');
            });

            $('.goods_detail_pic img').css({width:350, height:350});
        });
    </script>
{% endblock head %}
{% block detail1 %}
	<div class="goods_detail_con clearfix">
		<div class="goods_detail_pic fl"><img src="/static/{{ goods.gpic }}"></div>

		<div class="goods_detail_list fr">
			<h3>{{ goods.gtitle }}</h3>
			<p>{{ goods.gbrief }}</p>
			<div class="prize_bar">
				<span class="show_pirze">¥<em>{{ goods.gprice }}</em></span>
				<span class="show_unit">单  位：{{ goods.gunit }}</span>
                <span>库存：{{ goods.gstock }}</span>
			</div>
			<div class="goods_num clearfix">
				<div class="num_name fl">数 量：</div>
				<div class="num_add fl">
					<input type="text" class="num_show fl" value="1">
					<a href="javascript:add();" class="add fr">+</a>
					<a href="javascript:minus();" class="minus fr">-</a>
				</div> 
			</div>
			<div class="total">总价：<em>{{ goods.gprice }}元</em></div>
			<div class="operate_btn">
				<a href="javascript:;" class="buy_btn">立即购买</a>
				<a href="javascript:;" class="add_cart" id="add_cart">加入购物车</a>
			</div>
		</div>
	</div>
{% endblock detail1 %}

{% block detail2 %}
		<div class="r_wrap fr clearfix">
			<ul class="detail_tab clearfix">
				<li class="active">商品介绍</li>
				<li>评论</li>
			</ul>

			<div class="tab_content">
				<dl>
					<dt>商品详情：</dt>
					<dd>{{ goods.gcontent|safe }}</dd>
				</dl>
			</div>

		</div>
<div class="add_jump"></div>

<script type="text/javascript" src="/static/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    var $add_x = $('#add_cart').offset().top;
    var $add_y = $('#add_cart').offset().left;

    var $to_x = $('#show_count').offset().top;
    var $to_y = $('#show_count').offset().left;

    $(".add_jump").css({'left':$add_y+80,'top':$add_x+10,'display':'block'});
    $('#add_cart').click(function(){
        //判断如果未登录，则提示登录
        if($('.login_btn').text().indexOf('登录')>=0){
            alert('请先登录后再购买');
            location.href='/user/login/';
            return;
        }
        //动画
        $(".add_jump").stop().animate({
            'left': $to_y+7,
            'top': $to_x+7},
            "fast", function() {
                $(".add_jump").fadeOut('fast',function(){});
        });
        //异步
        num = parseInt($('.num_show').val());
        $.get('/cart/add{{ goods.id }}_'+num+'/', function(data){
            $('#show_count').text(data.count);
        });
    });
</script>
{% endblock detail2 %}
